<template>
  <div class="categorycontainer">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-button type="primary" @click="addcate">添加分类</el-button>
      <!-- 树形分类表格 -->
      <el-table
        :data="categoryList"
        style="width: 100%;margin-bottom: 20px;"
        row-key="cat_id"
        border
        :tree-props="{children: 'children'}"
        stripe
      >
        <el-table-column prop="cat_name" label="分类名称" width="180"></el-table-column>
        <el-table-column label="是否有效" width="180">
          <template slot-scope="scope">
            <i class="el-icon-success" v-if="!scope.row.cat_deleted" style="color:lime"></i>
            <i class="el-icon-error" v-else style="color:red"></i>
          </template>
        </el-table-column>
        <el-table-column prop="cat_level" label="排序">
          <template slot-scope="scope">
            <el-tag
              type="primary"
            >{{scope.row.cat_level === 0? "一级":scope.row.cat_level === 1? "二级":"三级"}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template>
            <!-- 编辑 -->
            <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
            <!-- 删除按钮 -->
            <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加分类对话框 -->
    <!-- 添加参数的对话框 -->
    <el-dialog title="添加分类" :visible.sync="addcateVisiable">
      <el-form :model="addCateData" :rules="addCateDataRules" ref="addCateRef" label-width="120px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateData.cat_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader
            @change='changeNode'
            :options="parentsNode"
            :props="selectConfig"
            v-model='selectedKey'
            clearable
            change-on-select
          ></el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addcateVisiable = false">取 消</el-button>
        <el-button type="primary" @click="addcateVisiable = false">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 查询分类信息
      queryInfo: {
        type: '',
        pagenum: 1,
        pagesize: 5
      },
      // 分类列表数据
      categoryList: [],
      // 添加分类对话框的显示与隐藏
      addcateVisiable: false,
      // 待添加的分类对象
      addCateData: {
        // 分类名
        cat_name: '',
        // 父类id
        cat_pid: 0,
        // 分类层级
        cat_level: 0
      },
      // 待添加的分类对象的校验规则
      addCateDataRules: {
        cat_name: [
          { required: true, message: '请输入添加的分类名', trigger: 'blur' }
        ]
      },
      // 父级分类对象
      parentsNode: [],
      // 级联选择器配置对象
      selectConfig: {
        value: 'cat_pid',
        label: 'cat_name',
        children: 'children',
        multiple: 'false',
        checkStrictly: 'true',
        emitPath: 'false' // 在选中节点改变时，是否返回由该节点所在的各级菜单的值所组成的数组，若设置 false，则只返回该节点的值,默认是true
      },
      // 选中的key
      selectedKey: []
    }
  },
  created () {
    this.getCategoryList()
  },
  methods: {
    changeNode () {
      console.log(this.selectedKey)
    },
    // 获取所有分类列表数据
    async getCategoryList () {
      const { data: res } = await this.$http.get('categories')
      this.categoryList = res.data
    },
    // 获取父级分类,只获取并渲染两层分类 第三层不能被选择
    async getParentNod () {
      const { data: res } = await this.$http.get('categories', {
        params: {
          type: 2
        }
      })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.parentsNode = res.data
    },
    // 添加分类
    addcate () {
      this.addcateVisiable = true
      this.getParentNod()
    }
  }
}
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
